What is the use of jquery .each() function? Manish Kumar206916-May-2017c#jquery Updated on 23-Sep-2020
Sarahh K.
27-Oct-2017The jQuery each method makes looping simpler. You can loop with the help of jQuery Each method anything like arrays, selector elements, etc.
The syntax of jQuery each method is:
$(selector).each(
function
(index,element))
Suppose you have an array, let me show you how to loop with
jQuery each method:
var
names = [
"a1"
,
"a2"
,
"a3"
,
"a4"
,
"a5"
];
$.each(names ,
function
(index, value){
console.log(
"Index is: "
+ index +
" :: Value is: "
+ value);
});For looping through selector elements with jQuery each:
$(
"div"
).each(
function
(index,value) {
//looping all div elements
}
Here I am looping through all the div elements of the DOM.
You you remove the div and give a class name and in that way you loop through every element having that class.
$(
".myclass"
).each(
function
(index,value) {
//looping all elements of that class
}
Manish Kumar
16-May-2017Basically the jQuery each() function is used to loop through each element of the targer jQuery object.Very useful for multi element DOM manipulation looping arrays and object properities
In this example alert box will be open 3 time